package top.buyibuyi.course.controller;

import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.buyibuyi.course.R;
import top.buyibuyi.course.esayExcel.ExcelStuSchose;
import top.buyibuyi.course.pojo.Admin;
import top.buyibuyi.course.pojo.PageBean;
import top.buyibuyi.course.pojo.StuSchose;
import top.buyibuyi.course.service.SelectStuChoseService;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@CrossOrigin
@RequestMapping("/achievement")
public class SelectStuChoseController {
    @Autowired
    private SelectStuChoseService selectStuChoseService;

    private List<StuSchose> stuSchoses;

    private List<StuSchose> stuSchoses2;

    @PostMapping("/selectStuChose/{page}/{limit}")
    public R selectStuChose(@PathVariable int page, @PathVariable int limit,
                            @RequestBody StuSchose schose){
        PageBean<StuSchose> adminPageBean = new PageBean<>();
        System.out.println("页面==》"+page+"页面大小==》"+limit+"条件==》"+schose);
        //对pageBean的七个属性赋值
        //页大小
        adminPageBean.setLimit(limit);
        //总数居条数
        int count = selectStuChoseService.selectCount(schose);
        System.out.println("kkkkkkk"+count);
        adminPageBean.setTotal(count);
        //设置当前页
        adminPageBean.setPage(page);
        //是否有上一页
        if(page>1){
            adminPageBean.setHasPrevPage(true);
        }else {
            adminPageBean.setHasPrevPage(false);
        }
        //是否有下一页
        if(adminPageBean.getPage() >= adminPageBean.getTotalPage()){
            adminPageBean.setHasNextPage(false);
        }else {
            adminPageBean.setHasNextPage(true);
        }
        //设置数据
        try {
            int startPage = 0;
            startPage = (page-1)*limit;
            System.out.println(startPage);
            System.out.println(schose.getSname());
            stuSchoses = selectStuChoseService.selectStuChose(startPage, limit, schose.getSname(), schose.getStuid());
            adminPageBean.setPageRecode(stuSchoses);
            System.out.println(stuSchoses);
            return R.ok().data("data",adminPageBean);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("....");
        }
    }

    @GetMapping("/excelWrite")
    public R excelWrite(){
        List<ExcelStuSchose> list = new ArrayList<ExcelStuSchose>();

        for (int i = 0; i < stuSchoses.size(); i++) {
            ExcelStuSchose data = new ExcelStuSchose();
            data.setStuid(stuSchoses.get(i).getStuid());
            data.setSname(stuSchoses.get(i).getSname());
            data.setCollege(stuSchoses.get(i).getChooseCourse().getCourse().getBelongto());
            data.setNowresults(stuSchoses.get(i).getNowresults());
            data.setMidresults(stuSchoses.get(i).getMidresults());
            data.setFinalresults(stuSchoses.get(i).getFinalresults());
            data.setSkillresults(stuSchoses.get(i).getSkillresults());
            data.setTotalresults(stuSchoses.get(i).getTotalresults());
            data.setIspass(stuSchoses.get(i).getIspass());
            list.add(data);
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        String fileName = "D:\\tomcat\\"+uuid+".xlsx";

        EasyExcel.write(fileName, ExcelStuSchose.class).sheet("学生成绩").doWrite(list);

        return R.ok();
    }


    @PostMapping("/selectCollegeStu/{page}/{limit}")
    public R selectCollegeStu(@PathVariable int page, @PathVariable int limit,
                              @RequestBody StuSchose schose){

        PageBean<StuSchose> adminPageBean = new PageBean<>();
        System.out.println("页面==》"+page+"页面大小==》"+limit+"条件==》"+schose);
        //对pageBean的七个属性赋值
        //页大小
        adminPageBean.setLimit(limit);
        //总数居条数
        int count = selectStuChoseService.selectCount(schose);
        System.out.println("kkkkkkk"+count);
        adminPageBean.setTotal(count);
        //设置当前页
        adminPageBean.setPage(page);
        //是否有上一页
        if(page>1){
            adminPageBean.setHasPrevPage(true);
        }else {
            adminPageBean.setHasPrevPage(false);
        }
        //是否有下一页
        if(adminPageBean.getPage() >= adminPageBean.getTotalPage()){
            adminPageBean.setHasNextPage(false);
        }else {
            adminPageBean.setHasNextPage(true);
        }
        //设置数据
        try {
            int startPage = 0;
            startPage = (page-1)*limit;
            System.out.println(startPage);
            System.out.println(schose.getSname());
            stuSchoses2 = selectStuChoseService.selectCollgeeStuChose(startPage, limit, schose.getSname(), schose.getStuid(),schose.getCollege());
            adminPageBean.setPageRecode(stuSchoses2);
            System.out.println(stuSchoses2);
            return R.ok().data("data",adminPageBean);
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("....");
        }
    }


    @GetMapping("/excelWrite2")
    public R excelWrite2(){
        List<ExcelStuSchose> list = new ArrayList<ExcelStuSchose>();

        for (int i = 0; i < stuSchoses2.size(); i++) {
            ExcelStuSchose data = new ExcelStuSchose();
            data.setStuid(stuSchoses2.get(i).getStuid());
            data.setSname(stuSchoses2.get(i).getSname());
            data.setCollege(stuSchoses2.get(i).getCollege());
            data.setNowresults(stuSchoses2.get(i).getNowresults());
            data.setMidresults(stuSchoses2.get(i).getMidresults());
            data.setFinalresults(stuSchoses2.get(i).getFinalresults());
            data.setSkillresults(stuSchoses2.get(i).getSkillresults());
            data.setTotalresults(stuSchoses2.get(i).getTotalresults());
            data.setIspass(stuSchoses2.get(i).getIspass());
            list.add(data);
        }
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        String fileName = "D:\\tomcat\\"+uuid+".xlsx";

        EasyExcel.write(fileName, ExcelStuSchose.class).sheet("学生成绩").doWrite(list);

        return R.ok();
    }
}
